package control;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Administrador;
import model.Aluno;
import model.Professor;
import model.Usuario;

/**
 * Servlet Filter implementation class SessaoFilter
 */
public class SessaoFilter implements Filter {

	private FilterConfig fc;
	private List<String> urlList = new ArrayList<String>();
	private List<String> urlListAlunos = new ArrayList<String>();
	private List<String> urlListProfessores = new ArrayList<String>();
	private List<String> urlListAdmin = new ArrayList<String>();
    /**
     * Default constructor. 
     */
    public SessaoFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		fc=null;
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		// place your code here

		// pass the request along the filter chain
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		String url_atual = req.getServletPath();
		HttpSession session=req.getSession(false);
		String url_redir="";
		boolean restricao = false;
		
		try{
			Usuario u = (Usuario) session.getAttribute("usuario");
			
			if (u instanceof Administrador){
				url_redir="painel.jsp";
				restricao = urlListAdmin.contains(url_atual);
			}
			else if (u instanceof Aluno){
				url_redir="alunos.jsp";
				restricao = urlListAlunos.contains(url_atual);
			}
			else if (u instanceof Professor){
				url_redir="professores.jsp";
				restricao = urlListProfessores.contains(url_atual);
			}
		}
		catch (Exception e) {
			url_redir="login.jsp";
			restricao=true;
		}		
			
		if (!urlList.contains(url_atual)){			
			if (restricao){
				res.sendRedirect(url_redir);
				return;
			}			
		}
		
		if (session!=null && url_atual.equals("/login.jsp")){
			res.sendRedirect(url_redir);
			return;
		}
		
		chain.doFilter(request, response);						
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		this.fc = fConfig;
		String urls = fc.getInitParameter("urllist");
		String urlsalu = fc.getInitParameter("urllistalu");
		String urlspro = fc.getInitParameter("urllistpro");
		String urlsadm = fc.getInitParameter("urllistadm");
		StringTokenizer tokenurls = new StringTokenizer(urls, ",");
		
        while (tokenurls.hasMoreTokens())
            urlList.add(tokenurls.nextToken());
        
        tokenurls = new StringTokenizer(urlsalu, ",");
        
        while (tokenurls.hasMoreTokens())
            urlListAlunos.add(tokenurls.nextToken());
        
        tokenurls = new StringTokenizer(urlspro, ",");
        
        while (tokenurls.hasMoreTokens())
            urlListProfessores.add(tokenurls.nextToken());
		
        tokenurls = new StringTokenizer(urlsadm, ",");
        
        while (tokenurls.hasMoreTokens())
            urlListAdmin.add(tokenurls.nextToken());
	}

}
